<template>
  <div class="style-page">
    <div class="head-page-title">{{ showComponentData.title }}</div>
    <div class="line"></div>
    <div class="head-title">组件样式</div>

    <div class="component-style">
      <el-form-item label="线条样式：">
        <el-radio-group v-model="showComponentData.layout">
          <el-radio value="solid">实线</el-radio>
          <el-radio value="dashed">虚线</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="线条高度：">
        <div class="flex">
          <el-slider style="width: 70%" v-model="form.itemStyle.height" :min="1" :max="100" />
          <el-input
            style="width: 30%; margin-left: 20px"
            type="number"
            v-model.number="form.itemStyle.height"
            :min="1"
            :max="100"
          >
            <template #append>px</template>
          </el-input>
        </div>
      </el-form-item>
      <el-form-item label="线条颜色：">
        <el-color-picker v-model="form.itemStyle.backgroundColor"></el-color-picker>
        <el-input style="width: 160px; margin-left: 10px" v-model="form.itemStyle.backgroundColor">
          <template #append>
            <el-button @click="form.itemStyle.backgroundColor = '#333333'">重置</el-button>
          </template>
        </el-input>
      </el-form-item>
    </div>

    <div class="component-style">
      <common-style
        :content-data="showComponentData"
        @update-style="handleUpdateStyle"
      ></common-style>
    </div>
  </div>
</template>

<script setup lang="ts">
  import CommonStyle from '@/views/decoration/diyComponents/CommonStyleComponent/CommonStyle.vue'
  import debounce from 'lodash/debounce'

  const props = defineProps({
    contentData: {
      type: Object,
      default() {
        return {}
      }
    }
  })

  const form = reactive({
    itemStyle: {
      backgroundColor: '#787878',
      height: 1
    }
  })

  watch(
    form,
    () => {
      emitFormUpdate()
    },
    { deep: true }
  )

  const emitFormUpdate = debounce(() => {
    const raw = toRaw(form)
    showComponentData.value.itemStyle = raw.itemStyle
    handleUpdateComponent()
  }, 300)

  const emit = defineEmits(['updateData'])

  const showComponentData = ref(props.contentData)

  function handleUpdateComponent() {
    emit('updateData', showComponentData.value)
  }

  function handleUpdateStyle(val: any) {
    showComponentData.value.style = val
    handleUpdateComponent()
  }

  onMounted(() => {
    if (showComponentData.value.itemStyle) {
      form.itemStyle.height = showComponentData.value.itemStyle.height
      form.itemStyle.backgroundColor = showComponentData.value.itemStyle.backgroundColor
    }
  })
</script>

<style scoped lang="scss">
  .style-page {
    padding: 20px;

    .head-page-title {
      padding-top: 10px;
      padding-bottom: 20px;
      font-size: 18px;
      font-weight: bold;
      color: black;
    }

    .head-title {
      padding: 20px 0;
      font-size: 14px;
      font-weight: bold;
      color: black;
    }

    .line {
      width: 100%;
      height: 1px;
      background: #f5f7fa;
    }

    .component-style {
      width: 100%;
      box-sizing: border-box;
      margin-top: 20px;

      .header {
        display: flex;
        justify-content: space-between;
        align-items: center;
        margin-bottom: 10px;

        .title {
          font-size: 16px;
          font-weight: bold;
        }
      }

      .flex {
        display: flex;
        justify-content: space-between;
        width: 100%;
        padding: 0 3px;
      }
    }
  }
</style>
